home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / Applications / ARTAbrot 1.0 / Enter_Coordinates.c < prev    next >
Text File  |  1993-08-23  |  13KB  |  317 lines

  1. /************************************************************************/
  2. /*                                                                        */
  3. /*    FILE:        Enter_Coordinates.c -- Code for entering coordinates    */
  4. /*                    to compute Mandelbrots.                                */
  5. /*                                                                        */
  6. /*    PURPOSE:    Code to get user input.                                    */
  7. /*                                                                        */
  8. /*    AUTHOR:        George T. Warner                                        */
  9. /*                                                                        */
  10. /*    REVISIONS:                                                            */
  11. /*    08/22/93    First version.                                            */
  12. /*                                                                        */
  13. /************************************************************************/
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include "ComUtil_ARTAbrot.h"    /* Common */
  19.  
  20. #include "AB_Alert.h"    /* Alert */
  21. #include "Enter_Coordinates.h"    /* This modeless dialog */
  22.  
  23. #pragma segment Enter_Coordinat
  24.  
  25. static Boolean        ExitDialog;                    /* Flag to exit dialog */
  26.  
  27. /* ======================================================= */
  28.  
  29. /* Routine: Init_Enter_Coordinat */
  30. /* Purpose: This procedures purpose is to set the window pointer to nil, */
  31. /* this is used to tell the other routines */
  32.  
  33. void Init_Enter_Coordinat()
  34. {
  35.     WPtr_Enter_Coordinat = NIL;                        /* Initialize to say that the dialog is not yet active */
  36. }
  37.  
  38. /* ======================================================= */
  39.  
  40. /* Routine: Moved_Enter_Coordinat */
  41. /* Purpose: We were moved, possibly to another screen and screen depth */
  42.  
  43. void Moved_Enter_Coordinat(WindowPtr theWindow)/* Moved this window */
  44. {
  45. WindowPtr    SavePort;                    /* Place to save the last port */
  46.  
  47.     if (WPtr_Enter_Coordinat == theWindow) {    /* Only do if the window is us */
  48.         GetPort(&SavePort);                    /* Save the current port */
  49.         SetPort(theWindow);                    /* Set the port to my window */
  50.  
  51.         SetPort(SavePort);                    /* Restore the old port */
  52.     }
  53. }
  54.  
  55. /* ======================================================= */
  56.  
  57. /* Routine: Update_Enter_Coordinat */
  58. /* Purpose: This procedures purpose is to refresh this window, update it, */
  59. /* when we are uncovered by another window.  */
  60.  
  61. void Update_Enter_Coordinat(WindowPtr theWindow)
  62. {
  63. GrafPtr    SavedPort;                                /* Save the current port so we can restore to it */
  64. Rect    tempRect;                                    /* Temporary rectangle variable */
  65. Rect    rTempRect;                                    /* Temporary rectangle variable */
  66. short    DType;                                    /* Type of dialog item */
  67. Handle    DItem;                                    /* Handle to the dialog item */
  68. RGBColor    Saved_ForeColor;                        /* Place to save colors */
  69. RGBColor    Saved_BackColor;                        /* Place to save colors */
  70. RGBColor    DrawingColor;                            /* Place to make colors */
  71.  
  72.     /* Only do if we are the window to update */
  73.     if ((WPtr_Enter_Coordinat != nil) && (theWindow == WPtr_Enter_Coordinat)) {
  74.         GetPort(&SavedPort);                            /* Get the current port */
  75.         SetPort(theWindow);                            /* Point to our port for drawing in our window */
  76.         GetForeColor(&Saved_ForeColor);            /* Save the fore color */
  77.         GetBackColor(&Saved_BackColor);            /* Save the back color */
  78.  
  79.         RGBForeColor(&Black_ForeColor);            /* Set the fore color to Black */
  80.         RGBBackColor(&White_BackColor);            /* Set the back color to White */
  81.  
  82.         GetDItem(theWindow,Res_Dlg_OK,&DType,&DItem,&tempRect);/* Get the item handle */
  83.         PenSize(3, 3);                                    /* Change pen to draw thick default outline */
  84.         InsetRect(&tempRect, -4, -4);                    /* Draw outside the button by 1 pixel */
  85.         FrameRoundRect(&tempRect, 16, 16);                /* Draw the outline */
  86.         PenSize(1, 1);                                 /* Restore the pen size to the default value */
  87.  
  88.         DrawingColor.red = 0xFFFF;  DrawingColor.green = 0x0000;  DrawingColor.blue = 0x0000;/* Set the color */
  89.         RGBForeColor(&DrawingColor);                /* Set the fore color */
  90.  
  91.         /* Draw a string of text, Static Text */
  92.         SetRect(&rTempRect, 44,108,157,123);
  93.         GetIndString(sTemp,Res_Dlg_Static_Text4,1);    /* Get the string */
  94.         TextBox(&sTemp[1], sTemp[0], &rTempRect,teJustLeft);
  95.  
  96.         RGBForeColor(&Black_ForeColor);            /* Set the fore color to Black */
  97.         DrawingColor.red = 0xFFFF;  DrawingColor.green = 0x0000;  DrawingColor.blue = 0x0000;/* Set the color */
  98.         RGBForeColor(&DrawingColor);                /* Set the fore color */
  99.         /* Draw a string of text, Static Text */
  100.         SetRect(&rTempRect, 109,78,159,93);
  101.         GetIndString(sTemp,Res_Dlg_Static_Text3,1);    /* Get the string */
  102.         TextBox(&sTemp[1], sTemp[0], &rTempRect,teJustLeft);
  103.  
  104.         RGBForeColor(&Black_ForeColor);            /* Set the fore color to Black */
  105.         DrawingColor.red = 0xFFFF;  DrawingColor.green = 0x0000;  DrawingColor.blue = 0x0000;/* Set the color */
  106.         RGBForeColor(&DrawingColor);                /* Set the fore color */
  107.         /* Draw a string of text, Static Text */
  108.         SetRect(&rTempRect, 15,48,155,63);
  109.         GetIndString(sTemp,Res_Dlg_Static_Text2,1);    /* Get the string */
  110.         TextBox(&sTemp[1], sTemp[0], &rTempRect,teJustLeft);
  111.  
  112.         RGBForeColor(&Black_ForeColor);            /* Set the fore color to Black */
  113.         DrawingColor.red = 0xFFFF;  DrawingColor.green = 0x0000;  DrawingColor.blue = 0x0000;/* Set the color */
  114.         RGBForeColor(&DrawingColor);                /* Set the fore color */
  115.         /* Draw a string of text, Static Text */
  116.         SetRect(&rTempRect, 15,18,155,33);
  117.         GetIndString(sTemp,Res_Dlg_Static_Text,1);        /* Get the string */
  118.         TextBox(&sTemp[1], sTemp[0], &rTempRect,teJustLeft);
  119.  
  120.         RGBForeColor(&Black_ForeColor);    /* Set the fore color to Black */
  121.         TextSize(12);
  122.         TextFont(systemFont);            /* Select the Font that we want */
  123.         TextFace(0);                    /* Select the style that we want */
  124.  
  125.         RGBForeColor(&Saved_ForeColor);    /* Restore the fore color */
  126.         RGBBackColor(&Saved_BackColor);    /* Restore the back color */
  127.  
  128.         DrawDialog(theWindow);            /* Draw the rest of the controls */
  129.         SetPort(SavedPort);                /* Restore the port that we saved at the start */
  130.     }
  131. }
  132.  
  133.  
  134. /* Routine: Open_Enter_Coordinat */
  135. /* Purpose: This procedures purpose is to open this window and set all */
  136. /* of the initial conditions, such as default edit text. */
  137.  
  138. void Open_Enter_Coordinat()
  139. {
  140. TEHandle    ThisEditText; 
  141. DialogPeek        TheDialogPtr;
  142. Rect    tempRect;                                    /* Temporary rectangle variable */
  143. short    DType;                                    /* Type of dialog item */
  144. char    temp_string[256];
  145. Handle    DItem;                                    /* Handle to the dialog item */
  146.  
  147.     if (WPtr_Enter_Coordinat == NIL) {
  148.         WPtr_Enter_Coordinat = GetNewDialog(Res_MD_Enter_Coordinat, NIL,  (WindowPtr)-1 );/* Bring in the dialog resource */
  149.         SetPort(WPtr_Enter_Coordinat);                    /* Prepare to add conditional text */
  150.         Doing_MovableModal = true;                        /* We are now in the movable modal mode */
  151.         TheDialogPtr = (DialogPeek)WPtr_Enter_Coordinat;/* Get to the inner record */
  152.         ThisEditText = TheDialogPtr->textH;            /* Get to the TE record */
  153.         HLock((Handle)ThisEditText);                    /* Lock it for safety */
  154.         (*ThisEditText)->txSize = 12;                    /* TE Point size */
  155.         TextSize(12);                                    /* Window Point size */
  156.         (*ThisEditText)->txFont = systemFont;            /* TE Font ID */
  157.         TextFont(systemFont);                            /* Window Font ID */
  158.         (*ThisEditText)->txFont = 0;                    /* TE Font ID */
  159.         (*ThisEditText)->fontAscent = 12;                /* Font ascent */
  160.         (*ThisEditText)->lineHeight = 12 + 3 + 1;        /* Font ascent + descent + leading */
  161.         HUnlock((Handle)ThisEditText);                    /* UnLock the handle when done */
  162.  
  163.  
  164.         /* Setup initial conditions */
  165.         /* Set initial X coordinate. */
  166.         GetDItem(WPtr_Enter_Coordinat,Res_Dlg_Edit_Text,&DType,&DItem,&tempRect);/* Get the item handle */
  167.         sprintf(&temp_string[1],"%f",fxcenter);    /* Get the string to display */
  168.         temp_string[0] = strlen(&temp_string[1]);
  169.         SetIText(DItem,temp_string);                            /* Set the default text string */
  170.  
  171.         /* Set initial Y coordinate. */
  172.         GetDItem(WPtr_Enter_Coordinat,Res_Dlg_Edit_Text2,&DType,&DItem,&tempRect);/* Get the item handle */
  173.         sprintf(&temp_string[1],"%f",fycenter);    /* Get the string to display */
  174.         temp_string[0] = strlen(&temp_string[1]);
  175.         SetIText(DItem,temp_string);                            /* Set the default text string */
  176.  
  177.         /* Set initial width. */
  178.         GetDItem(WPtr_Enter_Coordinat,Res_Dlg_Edit_Text3,&DType,&DItem,&tempRect);/* Get the item handle */
  179.         sprintf(&temp_string[1],"%f",fwidth);    /* Get the string to display */
  180.         temp_string[0] = strlen(&temp_string[1]);
  181.         SetIText(DItem,temp_string);                            /* Set the default text string */
  182.  
  183.         /* Set initial number of iterations. */
  184.         GetDItem(WPtr_Enter_Coordinat,Res_Dlg_Edit_Text4,&DType,&DItem,&tempRect);/* Get the item handle */
  185.         sprintf(&temp_string[1],"%d",fiters);    /* Get the string to display */
  186.         temp_string[0] = strlen(&temp_string[1]);
  187.         SetIText(DItem,temp_string);                            /* Set the default text string */
  188.  
  189.         SelIText(WPtr_Enter_Coordinat,Res_Dlg_Edit_Text4,0,300);
  190.  
  191.         ShowWindow(WPtr_Enter_Coordinat);        /* Open a dialog box */
  192.         SelectWindow(WPtr_Enter_Coordinat);        /* Lets see it */
  193.     }
  194.     else
  195.         SelectWindow(WPtr_Enter_Coordinat);        /* Lets see it */
  196. }
  197.  
  198. /* ======================================================= */
  199.  
  200. /* Routine: Close_Enter_Coordinat */
  201. /* Purpose: This procedures purpose is to close this window and clear */
  202. /* the window pointer variable */
  203.  
  204. void Close_Enter_Coordinat(WindowPtr theWindow)
  205. {
  206.     /* Only close if it is us and we were open */
  207.     if ((WPtr_Enter_Coordinat != NIL) && (theWindow == WPtr_Enter_Coordinat)) {
  208.         Doing_MovableModal = false;        /* We are now out of the movable modal mode */
  209.         DisposDialog(theWindow);        /* Close on the screen and Flush the dialog out of memory */
  210.         WPtr_Enter_Coordinat = nil;        /* Make sure our other routines know that we are closed */
  211.     }
  212. }
  213.  
  214. /* ======================================================= */
  215.  
  216.  
  217. /* Routine: Do_Enter_Coordinat */
  218. /* Purpose: This procedures purpose is to handle all actions, such as buttons being pressed. */
  219. /* This is the real meat of this unit and is where the code is for acting upon the users actions. */
  220.  
  221. void Do_Enter_Coordinat(EventRecord *theEvent,WindowPtr theWindow,short itemHit)
  222. {
  223. Point    myPt;                        /* For the local mouse position */
  224. Boolean    ExitDialog;                    /* Flag to close this dialog */
  225. short    DType;                        /* Type of dialog item */
  226. Handle    DItem;                        /* Handle to the dialog item */
  227. Rect    tempRect;                    /* Temporary rectangle */
  228. ControlHandle    CItem;                /* Control handle */
  229. short    temp;                        /* temp integer */
  230. char inputText[256];
  231. int        text_length;
  232.  
  233.     ExitDialog = false;                /* Do not close the dialog yet */
  234.     if ((theEvent->what == mouseDown) && (WPtr_Enter_Coordinat != nil)) {
  235.         SetPort(WPtr_Enter_Coordinat);    /* Set the port to our dialog */
  236.         myPt = theEvent->where;        /* Get the position where the mouse was pressed */
  237.         GlobalToLocal(&myPt);        /* Change from global to local location */
  238.     }
  239.  
  240.     if ((WPtr_Enter_Coordinat != nil)  && (WPtr_Enter_Coordinat  == theWindow)) {
  241.         if (theEvent->what == keyDown) {
  242.             itemHit = 0;                        /* Default to no item hit */
  243.             temp = (short)theEvent->message & charCodeMask;/* Get the character pressed */
  244.             if ((temp == 13) || (temp == 3)) {    /* See if CR or Enter */
  245.                 itemHit = 1;                    /* Default item hit */
  246.             }
  247.         }
  248.  
  249.         if ((WPtr_Enter_Coordinat != NIL)  && (WPtr_Enter_Coordinat  == theWindow)) {
  250.             myPt = theEvent->where;                /* Get the position where the mouse was pressed */
  251.             GlobalToLocal(&myPt);                /* Change from global to local location */
  252.  
  253.             GetDItem(theWindow, itemHit,&DType,&DItem,&tempRect);/* Get which item was pressed */
  254.             CItem = (ControlHandle)DItem;        /* Change the pointer for getting to the control */
  255.  
  256.             /* Handle it real time */
  257.             if (itemHit ==Res_Dlg_OK) {            /* Handle the Button being pressed */
  258.                 /* Get value entered for X coordinate. */
  259.                 GetDItem(WPtr_Enter_Coordinat,Res_Dlg_Edit_Text,&DType,&DItem,&tempRect);/* Get the item handle */
  260.                 GetIText(DItem,inputText);
  261.                 /* Convert to C string. */
  262.                 text_length = inputText[0];
  263.                 if (text_length<=254)
  264.                     text_length+=1;
  265.                 inputText[text_length] = 0;
  266.                 fxcenter = atof(&inputText[1]);
  267.  
  268.                 /* Get value entered for Y coordinate. */
  269.                 GetDItem(WPtr_Enter_Coordinat,Res_Dlg_Edit_Text2,&DType,&DItem,&tempRect);/* Get the item handle */
  270.                 GetIText(DItem,inputText);
  271.                 /* Convert to C string. */
  272.                 text_length = inputText[0];
  273.                 if (text_length<=254)
  274.                     text_length+=1;
  275.                 inputText[text_length] = 0;
  276.                 fycenter = atof(&inputText[1]);
  277.  
  278.                 /* Get value entered for width. */
  279.                 GetDItem(WPtr_Enter_Coordinat,Res_Dlg_Edit_Text3,&DType,&DItem,&tempRect);/* Get the item handle */
  280.                 GetIText(DItem,inputText);
  281.                 /* Convert to C string. */
  282.                 text_length = inputText[0];
  283.                 if (text_length<=254)
  284.                     text_length+=1;
  285.                 inputText[text_length] = 0;
  286.                 fwidth = atof(&inputText[1]);
  287.  
  288.                 /* Get value entered for number of iterations. */
  289.                 GetDItem(WPtr_Enter_Coordinat,Res_Dlg_Edit_Text4,&DType,&DItem,&tempRect);/* Get the item handle */
  290.                 GetIText(DItem,inputText);
  291.                 /* Convert to C string. */
  292.                 text_length = inputText[0];
  293.                 if (text_length<=254)
  294.                     text_length+=1;
  295.                 inputText[text_length] = 0;
  296.                 fiters = atoi(&inputText[1]);
  297.  
  298.                 new_coordinates=TRUE;
  299.  
  300.                 /* Close a modeless dialog */
  301.                 Add_UserEvent(UserEvent_Close_Window,Res_MD_Enter_Coordinat,0,0,nil);
  302.                 /* Open a Window */
  303.                 Add_UserEvent(UserEvent_Open_Window,Res_W_ARTAbrot,0,0,nil);
  304.             }
  305.  
  306.             if (itemHit ==Res_Dlg_Cancel) {        /* Handle the Button being pressed */
  307.                 Add_UserEvent(UserEvent_Close_Window,Res_MD_Enter_Coordinat,0,0,nil);/* Open a modeless dialog */
  308.             }
  309.         }
  310.     }
  311.  
  312.     if (ExitDialog) {                            /* Do the close of the dialog */
  313.         Close_Enter_Coordinat(WPtr_Enter_Coordinat);
  314.         WPtr_Enter_Coordinat = nil;                /* Clear it for future checks */
  315.     }
  316. }
  317.